// pages/mine/pages/my/afterSales/afterSales.js
const util = require('../../../../../utils/util');
const request = require('../../../../../utils/request')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page:1,
    navbarHeight: 0,
    limit:10,
    isLastPage:false,
    navlist: [
      {
          name: '待派单',
          status: 'undelivered',
          index: 0
      },
      {
          name: '配送完成',
          status: 'delivered',
          index: 1
      },
    ],
    //子导航选项卡-配送
    currentTab: 0,
    status: 'undelivered', //订单状态
    imageFiles:[],
    isShowReason: false,
    isShowPopup: false,
    complate_types:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      navbarHeight: util.getNavbarHeight(),
      userInfo: wx.getStorageSync('userInfo')
    })
    this.getList()

  },
  //获取上传图片
  getUploaderList(e) {
    console.log(e.detail.uploaderList)
    this.setData({
      imageFiles: e.detail.uploaderList
    })
  },
  closeReason(){
    this.setData({
      isShowReason: !this.data.isShowReason
    })
  },
  toCall(e) {
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function () {
        console.log('成功拨打电话')
      },
      fail(){}
    })
  },
  toBack() {
    wx.navigateBack()
  },
  tabClick(e) {
    const index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    this.setData({
      currentTab: index,
      status: item.status,
      page: 1,
      list: [],
      isLastPage: false,
    })
    this.getList()
  },
  hideClose(){
    this.setData({
      isShowPopup: !this.data.isShowPopup
    })
  },
  sureSend(e) {
    console.log(e)
    let order_id = e.currentTarget.dataset.item.order_id
    this.setData({
      order_id: order_id,
      isShowPopup: true
    })
    // request._post('/api?method=shequ.complete', {
    //   order_id
    // }).then(res => {
    //   getApp().toast(res.msg)
    //   setTimeout(function(){
    //     _this.setData({
    //       list:[],
    //       page:1,
    //       isLastPage:false
    //     })
    //     _this.getList()
    //   },1000)
    // })
  },
  getList() {
    let _this = this
    request._post('/api?method=shequ.orders', {
      page: this.data.page,
      status: this.data.status,
      limit: this.data.limit,
    }).then(res => {
      if (res.data.list.length == 0 || res.data.list == null) {
        _this.setData({
          isLastPage: true,
        })
      }
      _this.setData({
        complate_types: res.data.complate_types,
        list: _this.data.list.concat(res.data.list)
      })
      console.log(res,34343434)
    })
  },
  // 上拉加载
  onReachBottom: function () {
    if (this.data.isLastPage) {
      return
    }
    let page = this.data.page
    page++
    this.setData({
      page,
    })
    this.getList()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  submit(e) {
    let data = e.detail
    let _this = this
    data.order_id = this.data.order_id
    request._post('/api?method=shequ.complete', data).then(res => {
        getApp().toast(res.msg)
        _this.hideClose()
        _this.setData({
            list: [],
            page: 1,
            isLastPage: false,
        })
        setTimeout(function () {
          _this.getList()
        }, 1000)
    })
  }
})